import {login, logout, refreshToken} from '../../api/auth'
// 对cookie操作
import {PcCookie, Key} from '../../utils/cookie'

const state = {
    // 用户信息对象
    userInfo: PcCookie.get(Key.userInfoKey) ? PcCookie.get(Key.userInfoKey) : null,
    // 访问令牌字符串
    accessToken: PcCookie.get(Key.accessTokenKey),
    // 刷新令牌字符串
    refreshToken: PcCookie.get(Key.refreshTokenKey)
};

// 修改 state的值，该方法是指用来赋值操作，具体的行为是在actions中操作的
const mutations = {
    //  赋值用户状态
    SET_USER_STATE(state, data) {
        console.log('SET_USER_STATE', data);

        // 结构赋值
        const {userInfo, access_token, refresh_tokenKey} = data;

        state.userInfo = userInfo;
        state.accessToken = access_token;
        state.refreshToken = refresh_tokenKey;

        // 保存到cookie中
        PcCookie.set(Key.userInfoKey, userInfo);
        PcCookie.set(Key.accessTokenKey, access_token);
        PcCookie.set(Key.refreshTokenKey, refresh_tokenKey)
    },
    // 重置用户状态，退出和登录失败时用
    RESET_USER_STATE(state) {
        state.userInfo = null;
        state.accessToken = null;
        state.refreshToken = null;

        PcCookie.remove(Key.userInfoKey);
        PcCookie.remove(Key.accessTokenKey);
        PcCookie.remove(Key.refreshTokenKey);

    },


};

//定义行为
const actions = {

        // 登录操作
        UserLogin({commit}, userInfo) {
            const {username, password} = userInfo;

            return new Promise((resolve, reject) => {

                login({username: username.trim(), password: password}).then(response => {
                    const {code, data} = response;
                    // eslint-disable-next-line no-empty
                    if (code === 20000) {

                        commit("SET_USER_STATE", data);
                    }
                    resolve(response);

                }).catch(error => {
                    commit("RESET_USER_STATE");
                    reject(error)
                })
            })
        },
        // 推出操作
        UserLogout({state, commit}, redirectURL) {
            logout(state.accessToken).then(response => {
                // 重置状态
                commit("RESET_USER_STATE");
                // 重置状态后，回到传入进来的重定向地址
                window.location.href = redirectURL || '/';

            }).catch(error => {
                commit("RESET_USER_STATE");
                window.location.href = redirectURL || '/';
            })
        },
        SendRefreshToken({state, commit}) {
            return new Promise((resolve, reject) => {
                // 判断是否有刷新令牌
                if (!state.refreshToken) {
                    commit("RESET_USER_STATE");
                    reject("没有刷新令牌");
                    return;
                }

                refreshToken(state.refreshToken).then(response => {
                    commit("SET_USER_STATE", response.data);
                    resolve();
                }).catch(error => {
                    commit("RESET_USER_STATE");
                    reject(error);
                })

            })

        }
    }
;


export default {
    state,
    mutations,
    actions
}
